package view.Tabs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;

import view.General.CharacterInstance;
import view.Items.TopCharactersListGUI;
import view.Windows.LoginGUI;

import commons.Entities.LegenCharacter;
import commons.Entities.LegenCharacterThin;
import commons.Entities.User;

import controller.ControllerFunctions;

public class HomePageGUI {

	private final static HomePageGUI INSTANCE = new HomePageGUI();

	public static HomePageGUI get() {
		return INSTANCE;
	}

	public HomePageGUI() {

	}

	protected Shell shlMain;
	protected Color cBackgroundColor;
	protected Font fHeadFont;
	protected Font fLabelFont;
	protected Font fButtonFont;
	protected CTabFolder ctfHome;
	protected Display mainDisp;
	protected User user;
	protected Composite cHome;
	protected List<LegenCharacterThin> favoritesList;
	public Map<Integer, CharacterInstance> characterPages = new HashMap<Integer, CharacterInstance>();

	/**
	 * constructor
	 * 
	 * @param mainDisp
	 */
	public void HomePageGUIRun(final Display mainDisp, String username) {

		initFonts(mainDisp);
		setMainDisp(mainDisp);
		setUser(ControllerFunctions.get().getUserDetails(username));

		setFavoritesList(new ArrayList<LegenCharacterThin>());
		ControllerFunctions.get().getUserFavorites(getUser().getID());
		ControllerFunctions.get().getUserLegenDates(getUser().getID());

		shlMain = new Shell(mainDisp, SWT.SHELL_TRIM & ~SWT.RESIZE & ~SWT.MAX);
		shlMain.setBounds(10, 10, 900, 700);

		// locate the shell in the center of the display
		Monitor primary = mainDisp.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = shlMain.getBounds();
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;
		shlMain.setLocation(x, y);

		// main definitions
		shlMain.setText("LegenDate - Home");
		Image icon = new Image(mainDisp, getClass().getClassLoader().getResourceAsStream("view/Assets/LegenDate.png"));
		shlMain.setImage(icon);
		setBackgroundColor(new Color(mainDisp, 240, 240, 240));
		Color clrWhite = new Color(mainDisp, 255, 255, 255);
		final Image iBG = new Image(mainDisp, getClass().getClassLoader().getResourceAsStream("view/Assets/LegenDateBG2.jpg"));

		// the tab folder
		ctfHome = new CTabFolder(shlMain, 0);
		ctfHome.setBounds(0, 0, 900, 700);

		// the home tab
		CTabItem tiHome = new CTabItem(ctfHome, 0);
		tiHome.setText("Home");
		cHome = new Composite(ctfHome, 0);
		tiHome.setControl(cHome);
		cHome.setBackgroundImage(iBG);
		cHome.setBackgroundMode(SWT.INHERIT_DEFAULT);
		cHome.setForeground(clrWhite);

		// the profile tab
		final CTabItem tiProfile = new CTabItem(ctfHome, 0);
		tiProfile.setText("Profile");
		Composite cProfile = new Composite(ctfHome, 0);
		tiProfile.setControl(cProfile);
		cProfile.setBackgroundImage(iBG);
		cProfile.setBackgroundMode(SWT.INHERIT_DEFAULT);

		// the search tab
		final CTabItem tiSearch = new CTabItem(ctfHome, 0);
		tiSearch.setText("Search");
		Composite cSearch = new Composite(ctfHome, 0);
		tiSearch.setControl(cSearch);
		cSearch.setBackgroundImage(iBG);
		cSearch.setBackgroundMode(SWT.INHERIT_DEFAULT);

		// user name label button
		Label lblUserName = new Label(cHome, SWT.NONE);
		lblUserName.setText("Welcome " + user.getUserName() + "!");
		lblUserName.setBounds(50, 30, 300, 60);
		lblUserName.setFont(getHeadFont());
		lblUserName.setForeground(clrWhite);

		// switch to profile page button
		Button btnProfile = new Button(cHome, SWT.NONE);
		btnProfile.setFont(getButtonFont());
		btnProfile.setText("Profile");
		btnProfile.setBounds(30, 90, 200, 50);
		btnProfile.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				ctfHome.setSelection(tiProfile);
			}
		});

		// switch to search page button
		Button btnSearch = new Button(cHome, SWT.NONE);
		btnSearch.setFont(getButtonFont());
		btnSearch.setText("Search");
		btnSearch.setBounds(240, 90, 200, 50);
		btnSearch.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				ctfHome.setSelection(tiSearch);
			}
		});

		ProfileGUI.get().setMainDisp(mainDisp);
		ProfileGUI.get().setCompMain(cProfile);
		ProfileGUI.get().setUser(user);
		ProfileGUI.get().ProfileGUIRun(ctfHome);
		ProfileGUI.get().updateUserDetails();

		SearchGUI.get().setMainDisp(mainDisp);
		SearchGUI.get().setShlMain(cSearch);
		SearchGUI.get().setCtfHome(ctfHome);
		SearchGUI.get().SearchGUIRun();

		// open an 'add a new character' tab
		Button btnAddNewCharacter = new Button(cHome, SWT.NONE);
		btnAddNewCharacter.setFont(getButtonFont());
		btnAddNewCharacter.setText("Add New Character");
		btnAddNewCharacter.setBounds(450, 90, 200, 50);
		btnAddNewCharacter.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				CTabItem tiChar = new CTabItem(ctfHome, SWT.CLOSE);
				Composite cChar = new Composite(ctfHome, 0);
				tiChar.setControl(cChar);
				cChar.setBackgroundImage(iBG);
				cChar.setBackgroundMode(SWT.INHERIT_DEFAULT);
				tiChar.setText("New Character");
				AddCharacterGUI charGUI = new AddCharacterGUI(mainDisp, cChar, tiChar);
				ctfHome.setSelection(tiChar);

			}
		});

		// logout button
		Button btnLogout = new Button(cHome, SWT.NONE);
		btnLogout.setFont(getButtonFont());
		btnLogout.setText("Logout");
		btnLogout.setBounds(660, 90, 200, 50);
		btnLogout.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				shlMain.close();
				Shell newShell = new Shell(mainDisp, SWT.SHELL_TRIM & ~SWT.RESIZE);
				LoginGUI.get().LoginGUIRun(mainDisp, newShell);
			}
		});

		ControllerFunctions.get().getTopTenCharacters();
		ControllerFunctions.get().getBottomTenCharacters();
		ControllerFunctions.get().getTopTenReviewedCharacters();

	}

	public Color getBackgroundColor() {
		return cBackgroundColor;
	}

	public void setBackgroundColor(Color cBackgroundColor) {
		this.cBackgroundColor = cBackgroundColor;
	}

	public Font getHeadFont() {
		return fHeadFont;
	}

	public void setHeadFont(Font fHeadFont) {
		this.fHeadFont = fHeadFont;
	}

	public Font getLabelFont() {
		return fLabelFont;
	}

	public void setLabelFont(Font fLabelFont) {
		this.fLabelFont = fLabelFont;
	}

	public Font getButtonFont() {
		return fButtonFont;
	}

	public void setButtonFont(Font fButtonFont) {
		this.fButtonFont = fButtonFont;
	}

	public CTabFolder getCtfHome() {
		return ctfHome;
	}

	public void setCtfHome(CTabFolder ctfHome) {
		this.ctfHome = ctfHome;
	}

	public Display getMainDisp() {
		return mainDisp;
	}

	public void setMainDisp(Display mainDisp) {
		this.mainDisp = mainDisp;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Composite getcHome() {
		return cHome;
	}

	public void setcHome(Composite cHome) {
		this.cHome = cHome;
	}

	public List<LegenCharacterThin> getFavoritesList() {
		return favoritesList;
	}

	public void setFavoritesList(List<LegenCharacterThin> list) {
		this.favoritesList = list;
	}

	/**
	 * initialize fonts
	 * 
	 * @param display
	 */
	protected void initFonts(Display display) {
		setHeadFont(new Font(display, new FontData("Fiolex Girls", 20, SWT.BOLD)));
		setLabelFont(new Font(display, new FontData("Ariel", 10, SWT.BOLD)));
		setButtonFont(new Font(display, new FontData("Ariel", 10, SWT.BOLD)));

	}

	/**
	 * show the shell
	 * 
	 * @param display
	 */
	public void showGUI(Display display) {
		shlMain.open();
		// this is what's holding the shell open!!!
		while (!shlMain.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	/**
	 * open a character page
	 * 
	 * @param character
	 */
	public void openCharacterTab(final LegenCharacter character) {

		CTabItem tiChar = new CTabItem(ctfHome, SWT.CLOSE);
		Composite cChar = new Composite(ctfHome, 0);
		tiChar.setControl(cChar);
		tiChar.setText(character.getCharacterName());
		tiChar.addDisposeListener(new DisposeListener() {

			@Override
			public void widgetDisposed(DisposeEvent arg0) {
				characterPages.remove(character.getCharacterID());
			}

		});
		final CharacterGUI c = new CharacterGUI(mainDisp, cChar, character);
		CharacterInstance ciChar = new CharacterInstance(tiChar, c);
		characterPages.put(character.getCharacterID(), ciChar);
		ControllerFunctions.get().getCharacterReview(character.getCharacterID());
		ControllerFunctions.get().getCharacterComments(character.getCharacterID());
		ctfHome.setSelection(tiChar);

	}

	/**
	 * show the tables in the homepage
	 * 
	 * @param listHeader
	 * @param lctList
	 * @param x
	 * @param y
	 */
	public void showTopList(String listHeader, List<LegenCharacterThin> lctList, int x, int y) {
		new TopCharactersListGUI(listHeader, lctList, x, y, getcHome(), mainDisp, ctfHome);
	}
}
